#include "amarokbackend.h"

QDBusConnection bus = QDBusConnection::sessionBus();

// Marshall the DBusStatus data into a D-BUS argument
QDBusArgument &operator<<(QDBusArgument &argument, const DBusStatus &status)
{
    argument.beginStructure();
    argument << status.Play;
    argument << status.Random;
    argument << status.Repeat;
    argument << status.RepeatPlaylist;
    argument.endStructure();
    return argument;
}

// Retrieve the DBusStatus data from the D-BUS argument
const QDBusArgument &operator>>(const QDBusArgument &argument, DBusStatus &status)
{
    argument.beginStructure();
    argument >> status.Play;
    argument >> status.Random;
    argument >> status.Repeat;
    argument >> status.RepeatPlaylist;
    argument.endStructure();
    return argument;
}

uint AmarokBackend::features()
{
    return AUDIO_BACKEND_INFO | AUDIO_BACKEND_CONTROL;
}

AmarokBackend::AmarokBackend()
{
    qDBusRegisterMetaType<DBusStatus>();

    amarok_playpause = QDBusMessage::createMethodCall("org.kde.amarok", "/Player", "org.freedesktop.MediaPlayer", "PlayPause");
    amarok_backward = QDBusMessage::createMethodCall("org.kde.amarok", "/Player", "org.freedesktop.MediaPlayer", "Backward");
    amarok_forward = QDBusMessage::createMethodCall("org.kde.amarok", "/Player", "org.freedesktop.MediaPlayer", "Forward");
    amarok_getmetadata = QDBusMessage::createMethodCall("org.kde.amarok", "/Player", "org.freedesktop.MediaPlayer", "GetMetadata");
    amarok_mute = QDBusMessage::createMethodCall("org.kde.amarok", "/Player", "org.freedesktop.MediaPlayer", "Mute");
    amarok_next = QDBusMessage::createMethodCall("org.kde.amarok", "/Player", "org.freedesktop.MediaPlayer", "Next");
    amarok_prev = QDBusMessage::createMethodCall("org.kde.amarok", "/Player", "org.freedesktop.MediaPlayer", "Prev");
    amarok_pause = QDBusMessage::createMethodCall("org.kde.amarok", "/Player", "org.freedesktop.MediaPlayer", "Pause");
    amarok_play = QDBusMessage::createMethodCall("org.kde.amarok", "/Player", "org.freedesktop.MediaPlayer", "Play");
    amarok_repeat = QDBusMessage::createMethodCall("org.kde.amarok", "/Player", "org.freedesktop.MediaPlayer", "Repeat");
    amarok_volumeset = QDBusMessage::createMethodCall("org.kde.amarok", "/Player", "org.freedesktop.MediaPlayer", "VolumeSet");
    amarok_stop = QDBusMessage::createMethodCall("org.kde.amarok", "/Player", "org.freedesktop.MediaPlayer", "Stop");
    amarok_volumedown = QDBusMessage::createMethodCall("org.kde.amarok", "/Player", "org.freedesktop.MediaPlayer", "VolumeDown");
    amarok_volumeget = QDBusMessage::createMethodCall("org.kde.amarok", "/Player", "org.freedesktop.MediaPlayer", "VolumeGet");
    amarok_volumeup = QDBusMessage::createMethodCall("org.kde.amarok", "/Player", "org.freedesktop.MediaPlayer", "VolumeUp");
    amarok_getstatus = QDBusMessage::createMethodCall("org.kde.amarok", "/Player", "org.freedesktop.MediaPlayer", "GetStatus");

    qDebug() << bus.call(amarok_getstatus);
    QDBusReply< QDBusArgument > reply = bus.call(amarok_getstatus);
    QDBusArgument replyarg = reply.value();
    replyarg >> currentStatus;

    bus.connect("org.kde.amarok","/Player","org.freedesktop.MediaPlayer","StatusChange",this,SLOT(statusChange(DBusStatus)));
    bus.connect("org.kde.amarok","/Player","org.freedesktop.MediaPlayer","TrackChange",this,SLOT(trackChanged(QVariantMap)));
}

void AmarokBackend::trackChanged(QVariantMap metadata)
{
    emit this->songChanged();
}

void AmarokBackend::statusChange(DBusStatus status)
{
    this->currentStatus = status;

    if(currentStatus.Random == 1){
        emit randomStatusChanged(true);
    }else{
        emit randomStatusChanged(false);
    }

    if(currentStatus.Play == 0){
        emit stateChanged(true);
    }else{
        emit stateChanged(false);
    }

    if(currentStatus.RepeatPlaylist == 0){
        emit repeatStatusChanged(false);
    }else{
        emit repeatStatusChanged(true);
    }

}

QList<QString> AmarokBackend::getAllArtists()
{
    QList<QString> artists;



    return artists;

}

QList<Song*> * AmarokBackend::getAllSongs()
{
    QList<Song *> * songs = new QList<Song *>();

    return songs;
}

QList<QString> AmarokBackend::getAllAlbums()
{
    QList<QString> albums;

    return albums;
}

QList<QString> AmarokBackend::getAlbumsFromArtist(QString artist)
{
    QList<QString> albums;

    return albums;
}

QList<Song*> * AmarokBackend::getSongsFromAlbum(Album * album)
{
    QList<Song*> * songsList = new QList<Song*>();

    return songsList;
}

QList<Song*> * AmarokBackend::getSongsFromArtist(Artist * artist)
{
    QList<Song*> * songsList = new QList<Song*>();

    return songsList;
}

bool AmarokBackend::play()
{
    bus.call(amarok_play);
}

bool AmarokBackend::play(uint id)
{
    return false;
}

bool AmarokBackend::play(Song * song)
{
    return false;
}


bool AmarokBackend::stop()
{
    bus.call(amarok_stop);
    return true;
}

bool AmarokBackend::next()
{
    bus.call(amarok_next);
    return true;
}

bool AmarokBackend::prev()
{
    bus.call(amarok_prev);
    return true;
}

bool AmarokBackend::pause()
{
    bus.call(amarok_pause);
    return true;
}

bool AmarokBackend::togglePause()
{
    bus.call(amarok_playpause);
    return true;
}

bool AmarokBackend::setVolume(int value)
{
    QList<QVariant> args;
    args.append(QVariant(value));
    amarok_volumeset.setArguments(args);
    bus.call(amarok_volumeset);

    return true;
}

uint AmarokBackend::getVolume()
{
    QDBusMessage reply;
    reply = bus.call(amarok_volumeget);

    return reply.arguments().at(0).toUInt();
}

QString AmarokBackend::getCurrentSongTitle(){

    QDBusReply< QMap<QString,QVariant> > reply = bus.call(amarok_getmetadata);
    QString title = "";

    if (reply.isValid()) {
        QMap<QString,QVariant> map = reply.value();
        title = map.value("title").toString();
    }

    return title;
}

QString AmarokBackend::getCurrentSongArtist(){
    QDBusReply< QMap<QString,QVariant> > reply = bus.call(amarok_getmetadata);
    QString title = "";

    if (reply.isValid()) {
        QMap<QString,QVariant> map = reply.value();
        return map.value("artist").toString();
    }

    return title;
}

QString AmarokBackend::getCurrentSongAlbum(){
    QDBusReply< QMap<QString,QVariant> > reply = bus.call(amarok_getmetadata);
    QString title = "";

    if (reply.isValid()) {
        QMap<QString,QVariant> map = reply.value();
        return map.value("album").toString();
    }

    return title;
}

bool AmarokBackend::isPlaying()
{
    if(currentStatus.Play == 0){
        return true;
    }

    return false;
}

bool AmarokBackend::isPaused()
{
    if(currentStatus.Play == 0){
        return false;
    }

    return true;
}

bool AmarokBackend::seek(int sec)
{
    qDebug() << "Seeking not implemented";
    return false;
}

int AmarokBackend::getElapsedTime()
{
    qDebug() << "getElapsedTime not implemented";
    return false;
}

bool AmarokBackend::setRandom(bool status)
{
    qDebug() << "SetRandom not implemented";
    return false;
}

bool AmarokBackend::toggleRandom()
{
    qDebug() << "toggleRandom not implemented";
    return false;
}

bool AmarokBackend::getRandom()
{
    if(currentStatus.Random == 1){
        return true;
    }
    return false;
}

bool AmarokBackend::setRepeat(bool status)
{
    qDebug() << "setRepeat not implemented";
    return false;
}

bool AmarokBackend::toggleRepeat()
{
    qDebug() << "toggleRepeat not implemented";
    return false;
}


bool AmarokBackend::getRepeat()
{   if(currentStatus.RepeatPlaylist == 1){
        return true;
    }
    return false;
}

Q_EXPORT_PLUGIN2(tsmpc_AmarokBackend, AmarokBackend)
